<template>
  <div>
    <el-table :data="tableData" style="width: 100%;height:100%;">
      <el-table-column
          :prop="index"
          :label="item"
          v-for="(item, index) in tableHeader"
          :key="index"
      >
      </el-table-column>
      <el-table-column label="操作" fixed="right">
        <template #default="scope">
          <div v-if="scope.row.isEdit">
            <el-button type="primary" @click="handleRowConfirm(scope.row)">确定</el-button>
            <el-button type="danger" @click="handleRowCancel">取消</el-button>
          </div>
          <div v-else>
            <el-button type="primary" @click="handleRowEdit(scope.row)"> 详情</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {onMounted, reactive, toRefs} from "vue";
import {getModelPage10, getModelsColume, getModels, deleteModels} from "@/api/model";
import Vue from 'vue'
import { useRouter } from 'vue-router'
import { layer } from "@layui/layui-vue"

export default {
  name: "test",
  props: {
    modulename: String,
    title: String,
    content: String,
    orderfield: String,
    orderasc: String
  },
  methods: {
    handleRowEdit(row) {
      // 编辑操作
      layer.drawer({
        title: row["name"],
        content: row["content"]
      })
      //this.$router.push("/form_show?fields=content&modelname="+this.newmodlename+"&id="+row["id"]);
    },
    loaddata() {
      this.tableData = []
      var startIndex = ((this.currentPage - 1) * this.pageSize);
      const params = {
        order_column:this.orderfield,
        desc:this.orderasc,
        userid:this.userid,
        start:startIndex,
        end:this.pageSize
      }
      getModels(this.modulename,params).then(res => {
        for(let b=0;b<res.data.data.length;b++){
          this.tableData.push(res.data.data[b]);
        }
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.currentPage = 1;
      this.pageSize = val;
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.loaddata();
    }
  },
  data() {
    return {
      currentPage: 1, // 当前页码
      total: 20, // 总条数
      pageSize: 5, // 每页的数据条数
    };
  },
  setup(props) {
    console.log("----------------test img----------------------")
    console.log(props)
    const router = useRouter();
    const modelname = router.currentRoute.value.query.modelname;
    
    let newmodlename = "";
    if (typeof modelname === 'undefined') {
      newmodlename = props.modulename;
    }else{
      newmodlename = modelname;
    }
    var userid = ""
    if("userid" in router.currentRoute.value.query){
      userid = localStorage.getItem('UserId')
    }
    const state = reactive({
      tableHeader: {},
      tableData: [],
      numtotal: 0,
      columns: ["name","province","type",'public',"is_985",'is_211','is_two_first']
    })
    // 调取api获取数据
    const getTableName = () => {
      getModelsColume(newmodlename).then(res => {
        for(let i=0;i<res.data.length;i++){
          var obj = res.data[i];
          if(state.columns.includes(obj.Field)){
            state.tableHeader[obj.Field] = obj.Comment;
          }
        }
      })
      const params = {
        size:5,
        order_column:props.orderfield,
        desc:props.orderasc,
        userid:userid
      }
      console.log("=============newmodele===================")
      getModelPage10(newmodlename,params).then(res => {
        state.numtotal = res.data.num
        for(let m=0;m<res.data.data.length;m++){
          state.tableData.push(res.data.data[m])
        }
      })
    }
    onMounted(getTableName)
    // ...toRefs()将state里面得对象解构
    return {
      ...toRefs(state),
      newmodlename
    };
  }
}
</script>
